<?php 
if (!isset($_SESSION)) {
  session_start();
  if(!isset($_SESSION['MM_Username']))
	header("location:../../index.php");
}
?>
<?php 
	require_once('../../Connections/microassur.php'); 
	require_once('../../utils/fonctions.php');	
	require_once('inclure.php');
	$message = '';
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$currentPage = $_SERVER["PHP_SELF"];

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if( isset($_POST['Ligne1_adresse']) && empty($_POST['Ligne1_adresse']) ){
	$message = colorer("Vous devez saisir la premi&egrave;re ligne d&#39;adresse.","false");
}elseif(isset($_POST['Ligne1_adresse']) && caracteres_interdits($_POST['Ligne1_adresse'])==1){
	$message = colorer("La premi&egrave;re ligne adresse saisie comporte des caract&egrave;res sp&eacute;ciaux interdits.","false");
}elseif( isset($_POST['Ligne2_adresse']) && empty($_POST['Ligne2_adresse']) ){
	$message = colorer("Vous devez saisir la seconde ligne d&#39;adresse.","false");
}elseif(isset($_POST['Ligne2_adresse']) && caracteres_interdits($_POST['Ligne2_adresse'])==1){
	$message = colorer("La seconde ligne adresse comporte des caract&egrave;res sp&eacute;ciaux interdits.","false");
}elseif( isset($_POST['Ligne3_adresse']) && empty($_POST['Ligne3_adresse']) ){
	$message = colorer("Vous devez saisir au minimum la ville dans la troisi&egrave;me ligne d&#39;adresse.","false");
}elseif(isset($_POST['Ligne3_adresse']) && caracteres_interdits($_POST['Ligne3_adresse'])==1){
	$message = colorer("La troisi&egrave;me ligne adresse saisie comporte des caract&egrave;res sp&eacute;ciaux interdits.","false");
}else{
	if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
	  $insertSQL = sprintf("INSERT INTO adresse (Identifiant_adresse, Type_adresse, Etat_adresse, Ligne1_adresse, Ligne2_adresse, Ligne3_adresse, Identifiant_personne) VALUES (%s, %s, %s, %s, %s, %s, %s)",
						   GetSQLValueString($_POST['Identifiant_adresse'], "int"),
						   GetSQLValueString($_POST['Type_adresse'], "text"),
						   GetSQLValueString($_POST['Etat_adresse'], "text"),
						   GetSQLValueString($_POST['Ligne1_adresse'], "text"),
						   GetSQLValueString($_POST['Ligne2_adresse'], "text"),
						   GetSQLValueString($_POST['Ligne3_adresse'], "text"),
						   GetSQLValueString($_POST['Identifiant_personne'], "int")); //GetSQLValueString($_POST['Identifiant_personne'], "int"));
	
	  mysql_select_db($database_microassur, $microassur);
	  mysql_query("START TRANSACTION");
	  $Result1 = mysql_query($insertSQL, $microassur) or die(mysql_error());
	  if($Result1){
	  	mysql_query("commit");
		unset($_POST);
		$message .= colorer("L'adresse est cr&eacute;&eacute;e.","true");
	  }else{
		mysql_query("rollback");
		$message .= colorer("Probl&egrave;me cr&eacute;ation d\'adresse, contacter la DSI.","false");
	  }
	}
}

/**************************************************************************************/
$query_rs_dico1 = 'SELECT Identifiant_table FROM dictionnaire where Code_table="M0004"';
$rs_dico1 = mysql_query($query_rs_dico1, $microassur) or die(mysql_error());
$row_rs_dico1 = mysql_fetch_assoc($rs_dico1);
$num_row = $row_rs_dico1['Identifiant_table'];
/**************************************************************************************/
$query_rs_dico2 = "SELECT Code_element, Nom_element FROM elementdico where Identifiant_table = '$num_row'";
$rs_dico2 = mysql_query($query_rs_dico2, $microassur) or die(mysql_error());
$row_rs_dico2 = mysql_fetch_assoc($rs_dico2);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=8" />
			<title>Adresse</title>
			<link rel="stylesheet" type="text/css" href="../../css/main.css" />
            <link rel="stylesheet" type="text/css" href="list.css" />
			<!--[if IE 7]>
            	<link rel="stylesheet" type="text/css" href="../../css/ie7.css" />
            	<link rel="stylesheet" type="text/css" href="form_ie7.css" />
            <![endif]-->
	</head>
<body>
<div id="sf_menu1">
	<h3>Production <?php //echo nomPersonne($database_microassur, $microassur, $_GET['Identifiant_personne']) ?></h3>

<?php
	if(isset($_GET['msg']) && $_GET['msg']=='1')
		$message = colorer("Une personne doit avoir au moins une adresse.","false");
		
	if(isset($_GET['msg']) && $_GET['msg']=='supp')
		$message = colorer("Adresse supprim&eacute;e.","false");

	if(isset($message) && !empty($message)){
		echo $message;
	}
	$nommodule="Souscription contrat";
	//$nommodule2="Liste des adresse";
	$nom_module_saisie="D&eacute;tail adresse";
?>
</div>
<div class="cleared"></div>

<div class="corps1">
<!--/***************************************/-->
	<!--div class="imgleft"></div>
	<div class="imgmiddle">
		<div class="btn">
        	<span class="btnleft"></span>
            <span class="btnmiddle">
            	<h5><?php //echo $nommodule; ?></h5>
            </span><span class="btnright"></span>
        </div>
		<div class="btn1">
        	<span class="btnleft"></span>
            <span class="btnmiddle">
            	<h5><?php //echo $nommodule2; ?></h5>
            </span><span class="btnright"></span>
        </div>
		<div class="cleared"></div>
<table class="tab_list bordh">
  <tr id="niv1">
    <td class="col1">TYPE</td>
    <td class="col3">LIGNE 1</td>
    <td class="col4">LIGNE 2</td>
    <td class="col5">LIGNE 3</td>
  </tr>
</table>
    	<div class="content_table">
<table class="tab_list">
<?php $i=1; do 	{ 
		mysql_select_db($database_microassur, $microassur);
		$query_rs_lab_personne = "SELECT * FROM personne WHERE Identifiant_personne = '".$row_rs1['Identifiant_personne']. "'";
		$rs_lab_personne = mysql_query($query_rs_lab_personne, $microassur);
		$row_rs_lab_personne = mysql_fetch_assoc($rs_lab_personne);
		$totalRows_rs_lab_personne = mysql_num_rows($rs_lab_personne);
		
  ?>
<?php if ($totalRows_rs1 > 0) { // Show if recordset not empty ?>
    <tr <?php if($i%2==0) echo 'class="colorligne"'; ?>>
      <td class="col1"><a href="modadresse.php?Identifiant_adresse=<?php echo $row_rs1['Identifiant_adresse']; ?>&Identifiant_personne=<?php echo $_GET['Identifiant_personne']; ?>"><?php echo $row_rs1['Type_adresse']; ?></a></td>
      <td class="col3"><a href="modadresse.php?Identifiant_adresse=<?php echo $row_rs1['Identifiant_adresse']; ?>&Identifiant_personne=<?php echo $_GET['Identifiant_personne']; ?>"><?php echo $row_rs1['Ligne1_adresse']; ?></a></td>
      <td class="col4"><a href="modadresse.php?Identifiant_adresse=<?php echo $row_rs1['Identifiant_adresse']; ?>&Identifiant_personne=<?php echo $_GET['Identifiant_personne']; ?>"><?php echo $row_rs1['Ligne2_adresse']; ?></a></td>
      <td class="col5"><a href="modadresse.php?Identifiant_adresse=<?php echo $row_rs1['Identifiant_adresse']; ?>&Identifiant_personne=<?php echo $_GET['Identifiant_personne']; ?>"><?php echo $row_rs1['Ligne3_adresse']; ?></a></td>
    </tr>
    <?php 
	}
	$i++; } while ($row_rs1 = mysql_fetch_assoc($rs1)); ?>
</table>
		</div>
	</div>
	<div class="imgright"></div-->
	<div class="cleared"></div>
	<div id="br"></div>

<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
	<div class="imgleft"></div>
	<div class="imgmiddle">
    <div class="btn">
    	<span class="btnleft"></span>
        <span class="btnmiddle">
        	<h5><?php echo $nommodule; ?></h5>
        </span><span class="btnright"></span>
    </div>
  	<div class="btn1">
    	<span class="btnleft"></span>
        <span class="btnmiddle">
        	<h5><?php echo $nom_module_saisie; ?></h5>
        </span>
        <span class="btnright"></span>
    </div>
	<div class="formulaire">
  <table class="tabform">
    <tr>
      <td>TYPE ADRESSE</td>
      <td><select name="Type_adresse">
		<?php do { ?>
			<option value="<?php echo $row_rs_dico2['Code_element']; ?>" <?php echo remind_select('Type_adresse', $row_rs_dico2['Code_element']) ?> ><?php echo htmlentities($row_rs_dico2['Nom_element'], ENT_COMPAT, 'utf-8'); ?></option>
		<?php } while($row_rs_dico2 = mysql_fetch_assoc($rs_dico2)); ?>
      </select>
      </td>
    </tr>
	<input type="hidden" name="Etat_adresse" value="A"  />
    <tr>
      <td>ADRESSE</td>
      <td><input type="text" name="Ligne1_adresse" value="<?php echo remind_text('Ligne1_adresse'); ?>" size="32" maxlength="30" /></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="text" name="Ligne2_adresse" value="<?php echo remind_text('Ligne2_adresse'); ?>" size="32" maxlength="30" /></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="text" name="Ligne3_adresse" value="<?php echo remind_text('Ligne3_adresse'); ?>" size="32" maxlength="30" /></td>
    </tr>
	<input type="hidden" name="Identifiant_personne" value="<?php echo $_GET['Identifiant_personne']?>"  />
    <tr> </tr>
  </table>
    </div>
</div>
<div class="imgright"></div>				
<div class="cleared"></div>
    
<?php 
$nombreAdrActive = intval(nombreAdrActive($database_microassur, $microassur, $_GET['Identifiant_personne']));
if($nombreAdrActive>=1){ ?>
<div class="bt1">
	<a href="index.php?Identifiant_personne=<?php echo $_GET['Identifiant_personne']?>">
    	<span class="btleft"></span>
        <span class="btmiddle">Annuler</span>
        <span class="btright"></span>
    </a>
</div>
<?php }else{ ?>
<div class="bt1">
	<a href="../contrat/index.php?Identifiant_personne=<?php echo $_GET['Identifiant_personne']?>&msg=1">
    	<span class="btleft"></span>
        <span class="btmiddle">Annuler</span>
        <span class="btright"></span>
    </a>
</div>
<?php $msg = '1'; } ?>
<div class="bt">
	<span class="btleft"></span>
    <input type="submit" name="Valider" value="Valider" class="resizeall btmiddle"/>
    <span class="btright"></span>
</div>
    
<div class="cleared"></div>
  <input type="hidden" name="Identifiant_adresse" value="" />
  <input type="hidden" name="MM_insert" value="form1" />
</form>
 </div>
</body>
</html>
<?php

//mysql_free_result($rs1);

mysql_free_result($rs_lab_personne);

//mysql_free_result($rs_personne);
?>
